import os
# @Time   : 2022-03-03 17:00:00
# @Author : Gavin Jiang
# @Owner  : YSRD (Insights Value)

from flask import Response, g

from framework.interface.abstract_after_request import AbstractAfterRequest


class ClearInterceptor(AbstractAfterRequest):

    @staticmethod
    def priority():
        return 10

    def handle(self, resp: Response) -> Response:
        if hasattr(g, 'upload_tmp_files'):
            for item in g.upload_tmp_files:
                tmp_file_path = item.get('tmp_file_path')
                if item.get('clear_after_request', False) and os.path.exists(tmp_file_path):
                    os.remove(tmp_file_path)

        return resp

